package rules

import (
	v1 "golang-sgg-daijia/api/v1"
	"math"
)

// ProfitSharingRuleImplement 计算费用
type ProfitSharingRuleImplement struct {
}

func NewProfitSharingRuleImplement() *ProfitSharingRuleImplement {
	return &ProfitSharingRuleImplement{}
}

func (p *ProfitSharingRuleImplement) CalculateProfitSharing(req *v1.ProfitsharingRuleRequest) *v1.ProfitsharingRuleResponse {
	resp := &v1.ProfitsharingRuleResponse{}
	// 平台费率要先计算
	platformProfit := &PlatformProfit{}
	platformProfit.CalculateProfit(req, resp)

	profitSharingRule := newProfitSharingRule(req)
	profitSharingRule.CalculateProfit(req, resp)

	return resp
}

type ProfitSharingRule interface {
	CalculateProfit(req *v1.ProfitsharingRuleRequest, ProfitSharingRuleResp *v1.ProfitsharingRuleResponse)
}

func newProfitSharingRule(req *v1.ProfitsharingRuleRequest) ProfitSharingRule {
	if req.OrderAmount <= 100 && req.OrderNum <= 10{
		return &AmountLeq100AndNumLeq10{}
	} else if req.OrderAmount > 100 && req.OrderNum <= 10{
		return &AmountGe100AndNumLeq10{}
	} else if req.OrderAmount > 100 && req.OrderNum > 10{
		return &AmountGe100AndNumGe10{}
	} else {
		return &AmountLeq100AndNumGe10{}
	}
}

type PlatformProfit struct {
	/**
	  支付微信平台费用
	      平台费率：0.6%
	*/
}

func (p *PlatformProfit) CalculateProfit(req *v1.ProfitsharingRuleRequest, profitSharingRuleResp *v1.ProfitsharingRuleResponse) {
	profitSharingRuleResp.PaymentRate = 0.006
	profitSharingRuleResp.OrderAmount = req.OrderAmount
	paymentFee := math.Round(profitSharingRuleResp.OrderAmount * profitSharingRuleResp.PaymentRate * 100) / 100
	profitSharingRuleResp.PaymentFee = paymentFee
}

/**
订单金额小于等于100
    当天完成订单小于等于10单 平台抽成 20%
    当天完成订单大于10单 平台抽成 18%
*/

type AmountLeq100AndNumLeq10 struct {

}

func (p *AmountLeq100AndNumLeq10) CalculateProfit(req *v1.ProfitsharingRuleRequest, profitSharingRuleResp *v1.ProfitsharingRuleResponse) {
	totalAmount := profitSharingRuleResp.OrderAmount - profitSharingRuleResp.PaymentFee
	platformIncome := math.Round(totalAmount * 0.2 * 100) / 100
	driverTotalIncome := totalAmount - platformIncome
	// 代驾司机个税，税率：10%
	driverTaxFee := math.Round(driverTotalIncome * 0.1 * 100) / 100
	driverIncome := driverTotalIncome - driverTaxFee
	profitSharingRuleResp.PlatformIncome = platformIncome
	profitSharingRuleResp.DriverIncome = driverIncome
	profitSharingRuleResp.DriverTaxFee = driverTaxFee
	profitSharingRuleResp.DriverTaxRate = 0.1
}

type AmountLeq100AndNumGe10 struct {

}

func (p *AmountLeq100AndNumGe10) CalculateProfit(req *v1.ProfitsharingRuleRequest, profitSharingRuleResp *v1.ProfitsharingRuleResponse) {
	totalAmount := profitSharingRuleResp.OrderAmount - profitSharingRuleResp.PaymentFee
	platformIncome := math.Round(totalAmount * 0.18 * 100) / 100
	driverTotalIncome := totalAmount - platformIncome
	// 代驾司机个税，税率：10%
	driverTaxFee := math.Round(driverTotalIncome * 0.1 * 100) / 100
	driverIncome := driverTotalIncome - driverTaxFee
	profitSharingRuleResp.PlatformIncome = platformIncome
	profitSharingRuleResp.DriverIncome = driverIncome
	profitSharingRuleResp.DriverTaxFee = driverTaxFee
	profitSharingRuleResp.DriverTaxRate = 0.1
}

/**
订单金额大于100
    当天完成订单小于等于10单 平台抽成 18%
    当天完成订单大于10单 平台抽成 16%
*/

type AmountGe100AndNumLeq10 struct {

}

func (p *AmountGe100AndNumLeq10) CalculateProfit(req *v1.ProfitsharingRuleRequest, profitSharingRuleResp *v1.ProfitsharingRuleResponse) {
	totalAmount := profitSharingRuleResp.OrderAmount - profitSharingRuleResp.PaymentFee
	platformIncome := math.Round(totalAmount * 0.18 * 100) / 100
	driverTotalIncome := totalAmount - platformIncome
	// 代驾司机个税，税率：10%
	driverTaxFee := math.Round(driverTotalIncome * 0.1 * 100) / 100
	driverIncome := driverTotalIncome - driverTaxFee
	profitSharingRuleResp.PlatformIncome = platformIncome
	profitSharingRuleResp.DriverIncome = driverIncome
	profitSharingRuleResp.DriverTaxFee = driverTaxFee
	profitSharingRuleResp.DriverTaxRate = 0.1
}

type AmountGe100AndNumGe10 struct {

}

func (p *AmountGe100AndNumGe10) CalculateProfit(req *v1.ProfitsharingRuleRequest, profitSharingRuleResp *v1.ProfitsharingRuleResponse) {
	totalAmount := profitSharingRuleResp.OrderAmount - profitSharingRuleResp.PaymentFee
	platformIncome := math.Round(totalAmount * 0.16 * 100) / 100
	driverTotalIncome := totalAmount - platformIncome
	// 代驾司机个税，税率：10%
	driverTaxFee := math.Round(driverTotalIncome * 0.1 * 100) / 100
	driverIncome := driverTotalIncome - driverTaxFee
	profitSharingRuleResp.PlatformIncome = platformIncome
	profitSharingRuleResp.DriverIncome = driverIncome
	profitSharingRuleResp.DriverTaxFee = driverTaxFee
	profitSharingRuleResp.DriverTaxRate = 0.1
}


